package org.jupiter.blog.web.controller;

import jakarta.validation.Valid;
import org.jupiter.blog.api.dto.JupiterUserDTO;
import org.jupiter.blog.api.dto.PageResponseDto;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 *
 * @author lwl
 * @date 2024/8/3 下午7:16
 */
@RestController
@RequestMapping("/users")
public class JupiterUserController {

	@Autowired
	private JupiterUserService userService;

	/**
	 * 创建用户
	 */
	@PostMapping
	public JupiterResult<Void> createUser(@RequestBody @Valid JupiterUserDTO dto) throws Exception {
		userService.createUser(dto);
		return JupiterResult.success(null);
	}

	/**
	 * 根据UUID获取用户
	 */
	@GetMapping("/{uuid}")
	public JupiterResult<JupiterUserDTO> getUserById(@PathVariable("uuid") String uuid) {
		return JupiterResult.success(userService.getById(uuid));
	}

	/**
	 * 根据用户名或邮箱获取用户
	 */
	@GetMapping("/search")
	public JupiterResult<JupiterUserDTO> getUserByUsernameOrEmail(
			@RequestParam(required = false) String username,
			@RequestParam(required = false) String email) {
		return JupiterResult.success(userService.getByUsernameOrEmail(username, email));
	}

	/**
	 * 更新用户信息
	 */
	@PutMapping("/{uuid}")
	public JupiterResult<Void> updateUser(@PathVariable("uuid") String uuid, @RequestBody @Valid JupiterUserDTO dto)
			throws Exception {
		userService.updateUser(uuid, dto);
		return JupiterResult.success(null);
	}

	/**
	 * 删除用户
	 */
	@DeleteMapping("/{uuid}")
	public JupiterResult<Void> deleteUser(@PathVariable("uuid") String uuid) {
		userService.deleteUser(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 分页查询用户列表
	 */
	@PostMapping("/page")
	public JupiterResult<PageResponseDto<JupiterUserDTO>> getUsersByPage(@RequestBody JupiterUserDTO queryDTO) {
		List<JupiterUserDTO> users = userService.getByPage(queryDTO);
		long total = userService.getCount(queryDTO);
		return JupiterResult.page(users, total);
	}

	/**
	 * 更新用户状态
	 */
	@PutMapping("/{uuid}/status")
	public JupiterResult<Void> updateUserStatus(@PathVariable("uuid") String uuid, @RequestParam Integer status)
			throws Exception {
		userService.updateStatus(uuid, status);
		return JupiterResult.success(null);
	}

	/**
	 * 重置用户密码
	 */
	@PutMapping("/{uuid}/password/reset")
	public JupiterResult<Void> resetPassword(@PathVariable("uuid") String uuid) throws Exception {
		userService.resetPassword(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 恢复用户
	 */
	@RequestMapping(value = "/restoreUser/{uuid}", method = RequestMethod.POST)
	public JupiterResult<Void> restoreUser(@PathVariable("uuid") String uuid) {
		userService.restoreUser(uuid);
		return JupiterResult.success(null);
	}

	/**
	 * 检查用户名是否存在
	 */
	@GetMapping("/check/username")
	public JupiterResult<Boolean> checkUsername(@RequestParam String username) {
		return JupiterResult.success(userService.existsByUsername(username));
	}

	/**
	 * 检查邮箱是否存在
	 */
	@GetMapping("/check/email")
	public JupiterResult<Boolean> checkEmail(@RequestParam String email) {
		return JupiterResult.success(userService.existsByEmail(email));
	}
}
